import os

class CalFileSize:
    """计算文件大小"""
    def __init__(self):
        pass

    def getAllFileSize(self, dirPath):
        """获取目录下所有文件大小总和"""
        if os.path.isdir(dirPath):
            return self.__calculateAllFileSize(dirPath)
        else:
            print("您传入的路径不是一个目录")
            return os.path.getsize(dirPath)

    def __calculateAllFileSize(self, dirPath):
        totalFileSize = 0
        files = os.listdir(dirPath)
        for file in files:
            f_path = os.path.join(dirPath, file)
            print("__calculateAllFileSize f_path:", f_path)
            # 去除掉隐藏文件
            if not file.startswith("."):
                # 如果发现是个目录，继续在该目录中查找计算
                if os.path.isdir(f_path):
                    totalFileSize += self.__calculateAllFileSize(f_path)
                else:
                    # 如果是文件就累加文件大小
                    totalFileSize += os.path.getsize(f_path)
        return totalFileSize
